fix(text): preserve stream order for RTL-placed design-tool PDFs#25
Merged
Mythie merged 1 commit intoLibPDF-js:mainfrom Feb 12, 2026
Merged
Conversation
Contributor
|
@ljagiello is attempting to deploy a commit to the mythie's projects Team on Vercel. A member of the Team first needs to authorize it. |
0630cef to
d4634cb
Compare
Design tools like Figma and Canva emit LTR text with right-to-left TJ
placement. Sorting by x-position reverses the text. Detect this pattern
via sequenceIndex and preserve content stream order instead.
- Add RTL_PLACED_THRESHOLD constant (0.8) with documentation
- Return OrderedLine { chars, rtlPlaced } from orderLineChars
- Fix gap calculation in groupIntoSpans for RTL-placed lines
- Fix createSpaceChar bbox positioning for RTL-placed lines
- Use fractional sequenceIndex (n + 0.5) for synthetic spaces
- Make sequenceIndex optional on ExtractedChar
- Guard against missing sequenceIndex (fall back to x-sort)
- Document that heuristic correctly handles genuine RTL text
- Document mixed bidi limitation (needs full bidi algorithm)
- Add 12 unit tests for RTL-placed detection edge cases
d4634cb to
dc35d21
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
sequenceIndexfield toExtractedCharto preserve content stream extraction orderProblem
Design tools export PDFs where characters have near-zero glyph widths and all positioning is done via positive TJ adjustments (which move the pen left). Characters appear in correct reading order in the content stream, but their x-positions decrease monotonically. The line grouper unconditionally sorted characters by x-position, reversing the text.
For example,
"Lorem ipsum dolor sit amet"would be extracted as"tema tis rolod muspi meroL".Approach
Rather than removing position-based sorting entirely (which could break multi-column or out-of-order PDFs), we detect RTL-placed lines by checking if x-positions in stream order are predominantly decreasing (>= 80% of consecutive pairs). When detected, we preserve content stream order. For normal PDFs where stream order and position order agree, behavior is unchanged.
This approach is informed by how other PDF libraries handle this:
sortByPosition)Test plan
src/integration/text/rtl-placed-text.test.tsverifies correct extraction from RTL-placed fixtureFixes #24